package useranswer

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go-interview-forum/resp"
	"go-interview-forum/service/useranswer"
	"strconv"
)

type UserCreateAnswerReq struct {
	InterviewsId int64 `json:"interviews_id"`
	PId          int64 `json:"p_id"`
}

// 添加刷题记录
func UserAnswerCreate(c *gin.Context) {
	data, exists := c.Get("userId")
	if !exists {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "系统错误")
		return
	}

	userId, err := strconv.Atoi(data.(string))
	if err != nil {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "系统错误")
		return
	}

	var u UserCreateAnswerReq
	err = c.ShouldBind(&u)
	if err != nil {
		resp.QueryErrorResp(c, err.Error())
		return
	}

	err = useranswer.AnswerCreateRecord(int64(userId), u.InterviewsId, u.PId)
	if err != nil {
		resp.QueryErrorResp(c, err.Error())
		return
	}
	resp.SuccessResp(c, err)
}

type UserAnswerReqLIst struct {
	PId      int64 `json:"p_id"`
	Page     int   `json:"page"`
	PageSize int   `json:"page_size"`
}

// // 刷题添加记录列表
func UserAnswerList(c *gin.Context) {
	data, exists := c.Get("userId")
	if !exists {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "系统错误")
		return
	}

	userId, err := strconv.Atoi(data.(string))
	if err != nil {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "系统错误")
		return
	}

	var u UserAnswerReqLIst
	err = c.ShouldBind(&u)
	if err != nil {
		resp.QueryErrorResp(c, err.Error())
		return
	}

	list, err := useranswer.AnswerRecordList(int64(userId), u.PId)
	if err != nil {
		resp.QueryErrorResp(c, err.Error())
		return
	}
	resp.SuccessResp(c, list)
}

// 用户刷题默认记录列表
func UserAnswerListAll(c *gin.Context) {
	data, exists := c.Get("userId")
	if !exists {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "系统错误")
		return
	}

	userId, err := strconv.Atoi(data.(string))
	if err != nil {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "系统错误")
		return
	}
	var u UserAnswerReqLIst
	err = c.ShouldBind(&u)
	if err != nil {
		fmt.Println("shouldbind", err)
		resp.QueryErrorResp(c, err.Error())
		return
	}
	list, err := useranswer.AnswerRecordListAll(int64(userId), u.PId, u.Page, u.PageSize)
	if err != nil {
		resp.QueryErrorResp(c, err.Error())
		return
	}

	resp.SuccessResp(c, list)
	return
}

type UserAnswerDelete struct {
	Id  []int64 `json:"id"`
	PId int64   `json:"p_id"`
}

// 用户刷题删除
func AnswerDelete(c *gin.Context) {
	data, exists := c.Get("userId")
	if !exists {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "系统错误")
		return
	}

	userId, err := strconv.Atoi(data.(string))
	if err != nil {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "系统错误")
		return
	}

	var u UserAnswerDelete
	err = c.ShouldBind(&u)
	if err != nil {

		return
	}

	err = useranswer.UserAnswerDelete(u.Id, int64(userId), u.PId)
	if err != nil {
		resp.QueryErrorResp(c, err.Error())
		return
	}
	resp.SuccessResp(c, nil)
}
